TicketMaster API
HTML request message
- Request structure
GET /doc/test.html HTTP 1.1
First part indicates the type of the request, second part indicates the resource it asks for, third part is the version of protocal- Request Header
- Request Body
bookId=12345&author=Tan+Ah+Teck
- Some browser will process the data here.
- Response structure
- Some browser will process the data here.
- Status line
HTTP/1.1 200 OK
200 indicates that the response status number, and the “OK” is the explanation of the status. - Response Header
- Response Body
<h1>Home page</h1>
RESTful API
- CRUD
- Create/Read/Update/Delete
- matches HTTP POST/GET/PUT/DELETE
- server is stateless
- No need to involve other requests when handling the request.
TicketMaster API
- Request
- URL of discover API
protocol://hostname:port/endpoint?query
:- protocol:
https
- hostname:
app.ticketmaster.com
- endpoint:
/discovery/v2/events.json
- query:
- APIkey: it’s required by TicketMaster API for
authn/authz
- GeoPoint:
lat/long
since our search is based on client location - Radius: radius of search area
- Keyword: search a specific kind of events
- APIkey: it’s required by TicketMaster API for
- protocol:
- example
https://app.ticketmaster.com/discovery/v2/events.json?apikey=12345&geoPoint=abcd&keyword=music&radius=50
- Headers and Body: nothing to set since it’s not required for this API.
- URL of discover API
- Response
- data we need is located in
_embedded(JSON Object) -> events(JSON Array) -> item object(JSON Object)
- data we need is located in
Implementation
- Create a new Package
external
in src - Create a new class
TicketMasterAPI
Add some constants
1
2
3private static final String URL = "https://app.ticketmaster.com/discovery/v2/events.json";
private static final String DEFAULT_KEYWORD = ""; // no restriction
private static final String API_KEY = "USE_YOUR_OWN_KEY";Add a search function
1
2
3
4
5
6
7
8
9/**
* Search the keyword through TM API
* @param lat latitude
* @param lon longitude
* @param keyword keyword is optional
*/
public JSONArray search(double lat, double lon, String keyword) {
return null;
}Add queryAPI helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/**
* print the results of search(lat, lon)
* @param lat latitude
* @param lon longitude
*/
private void queryAPI(double lat, double lon) {
JSONArray events = search(lat, lon, null);
try {
for (int i = 0; i < events.length(); i++) {
JSONObject event = events.getJSONObject(i);
System.out.println(event);
}
} catch (Exception e) {
e.printStackTrace();
}
}Add GeoHash helper to convert lat, lon to geoPoint. Copy the code from
http://developer-should-know.com/post/87283491372/geohash-encoding-and-decoding-algorithm
Implement search(lat,lon,keyword) function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58/**
* Search the JSONArray results through TM API
* @param lat latitude
* @param lon longitude
* @param keyword keyword is optional
* @return JSONArray
*/
public JSONArray search(double lat, double lon, String keyword) {
JSONArray ret = new JSONArray();
if (keyword == null) keyword = DEFAULT_KEYWORD;
// translate keyword into URL-supported format
try {
keyword = java.net.URLEncoder.encode(keyword, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
// Get geoPoint
String geoHash = GeoHash.encodeGeohash(lat, lon, 8);
// Create query
// 50 is default search radius
String query = String.format("apikey=%s&geoPoint=%s&keyword=%s&radius=%s", API_KEY, geoHash, keyword, 50);
// Create URL
try {
// create a HTTP URL connection
HttpURLConnection connection = (HttpURLConnection) new URL(URL + "?" + query).openConnection();
// get the response code EG. 200/success, 404/fail
int responseCode = connection.getResponseCode();
// print res
System.out.println("\nSending \"GET\" request to URL : " + URL + "?" + query);
System.out.println("\nResponse Code: " + responseCode);
// check responseCode (Implement it later)
if (responseCode != 200) {
}
// read and write the response content
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
connection.disconnect();
// write the response to a JSON object
JSONObject obj = new JSONObject(response.toString());
// check the result
if (obj.isNull("_embedded")) {
return ret;
}
// get the events from the whole JSON and return the events field of it as a JSON Array.
JSONObject embedded = obj.getJSONObject("_embedded");
JSONArray events = embedded.getJSONArray("events");
ret = events;
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}Create a new package
entity
and a new classItem
(entity表示servelet储存数据的类)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27/**
* Item of event
* @author andaluo
*
*/
public class Item {
private String itemId;
private String name;
private double rating;
private String address;
private Set<String> categories;
private String imageURL;
private String url;
private double distance;
public JSONObject toJSONObject() {
JSONObject obj = new JSONObject();
try {
obj.put("item_id", itemId);
obj.put("name", name);
obj.put("rating", rating);
// ...
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}Use builder pattern to create Item class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43/**
* A helper class to build the Item class
* @author andaluo
*
*/
public static class ItemBuilder{
private String itemId;
private String name;
// ...
/**
* Construct the Item using builder
* @return
*/
public Item build() {
return new Item(this);
}
/**
* return the builder itself to support continuous operation.
* @param itemId
* @return
*/
public ItemBuilder setItemId(String itemId) {
this.itemId = itemId;
return this;
}
//...
}
/**
* Builder pattern to create a class
* @param builder
*/
public Item(ItemBuilder builder) {
this.itemId = builder.itemId;
this.name = builder.name;
// ...
// when build the item we get
public static void main(String[] args) {
Item item = new Item.ItemBuilder().setAddress("abc").setDistance(19).build();
}
- Create a new Package